package com.air.repo.design.pattern.proxy;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @Description: java类作用描述
 * @Author: yh
 * @CreateDate: 2018/7/11 15:24
 */
public class AssistantDynamicCglib2 {

    Boss boss;
    public AssistantDynamicCglib2(Boss boss) {
        this.boss = boss;
    }


    public Object getBossProxy(){
        Enhancer enhancer=new Enhancer();
        enhancer.setSuperclass(boss.getClass());
        enhancer.setCallback(new MethodInterceptor() {
            public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
                return method.invoke(boss,args);
            }
        });
        return enhancer.create();

    }
    public static void main(String[] args) throws Throwable{
        Boss boss=new BossImpl();
        System.out.println("代理之前>>>>"+boss.getClass());
        Boss assistant=(Boss)new AssistantDynamicCglib2(boss).getBossProxy();

        System.out.println("代理之后>>>>"+assistant.getClass());
        assistant.getTea();
    }
}
